package org.shilong.UI.model.friend;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.shilong.Utilitys.Strings.Constants;
import org.shilong.mContext.db.DBContext;
import org.shilong.model.BusinessCardModel;

import com.url.ajax.json.JSONArray;
import com.url.ajax.json.JSONException;
import com.url.ajax.json.JSONObject;

public class QueryBussinesCardByIDAction extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public QueryBussinesCardByIDAction() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("content-type", "text/html;charset=UTF-8");

		String weiboid = request.getParameter("weibo_id");
		
		String rlt = "";
		
		rlt = getJson(getRlt( weiboid ));
		PrintWriter out = response.getWriter();
		
		out.print( rlt );
		out.flush();
		out.close();
		
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

	
	private String getJson( List<BusinessCardModel> ll ){
		
		JSONObject jon = new JSONObject();
		JSONArray arr = new JSONArray();
		
		Iterator<BusinessCardModel> it = ll.iterator();
		
		String rlt ="";
		
		while( it.hasNext()){
			
			JSONObject oo = new JSONObject();
			BusinessCardModel bean  =  it.next();
	
			
			try {
				oo.put( "account_name" ,  bean.getAccount_name() );
				oo.put( "age" ,  bean.getAge() );
				oo.put( "company_name" ,  bean.getCompany_name() );
				oo.put( "domain" ,  bean.getDomain() );
				oo.put( "sex" ,  bean.getSex() );
				oo.put( "telephone_number" ,  bean.getTelephone_number() );
				oo.put( "bussiness" ,  bean.getBussiness() );
				
				arr.put( oo );
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
			
		}
		
		try {
			jon.put("rlt", arr );
			
			rlt = jon.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rlt;
	}
	private static List<BusinessCardModel> mList = null;

	public static List<BusinessCardModel> getRlt(String weiboid) {

		mList = null;

		mList = new ArrayList<BusinessCardModel>();

		Connection conn = null;

		CallableStatement cst = null;

		ResultSet rs = null;

		conn = DBContext.createInstance(DBContext.TYPE_MYSQL).getConnector();

		try {
			cst = conn.prepareCall("{call proc_queryBussinesCardByID(?)}");

			cst.setString(1, weiboid );

			rs = cst.executeQuery();

			while (rs.next()) {

				BusinessCardModel bean = new BusinessCardModel();

				bean.setAccount_name( rs.getString( "account_name" ) );
				bean.setAge( rs.getString( "age" ) );
				bean.setBussiness( rs.getString( "bussiness" ) );
				bean.setCompany_name( rs.getString( "company_name" ) );
				bean.setDomain( rs.getString( "domain" ) );
				bean.setSex( rs.getString( "sex" ) );
				bean.setTelephone_number(  rs.getString( "telephone_number" ) );

				mList.add(bean);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

			try {

				if (rs != null) {
					rs.close();
					rs = null;
				}

				if (cst != null) {
					cst.close();
					cst = null;
				}

			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		return mList;
	}
}
